/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;
import java.lang.Object;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author WELCOME
 */
public class TCPClient {
    public static Socket clientSocket;
    String receivedMessage;                     // nhan duoc tu server
    
    InputStream is;
    BufferedReader br;
    
    OutputStream os;
    BufferedWriter bw;
    
    ObjectInputStream oInput;
    ObjectOutputStream oOutput;
    
    public static Vector<String> kqua = new Vector<String>();
    public static Vector<Vector<String>>lstThuocTinh = new Vector<Vector<String>>();
    public static Vector<Vector<String>>lstData = new Vector<Vector<String>>();
    public static int kq;
    public static int flag;
    public static String msg;
    public TCPClient(Socket clientSocket){
        this.clientSocket = clientSocket;
    }
    
    public TCPClient(InetAddress  host, int port){
        try {
            clientSocket = new Socket(host, port);
            //JOptionPane.showMessageDialog(null, "Connect successfull!");
            System.out.println("Talking to Server");
            oOutput = new ObjectOutputStream(clientSocket.getOutputStream());
            oInput = new ObjectInputStream(clientSocket.getInputStream());
            Thread t = new Thread(){
                    public void run(){
                        while (true) {   
                            try {
                                // ket qua cau lenh select
                                int maCode = (Integer) oInput.readObject();
                                switch (maCode) {
                                    case 0:
                                        lstData = (Vector<Vector<String>>) oInput.readObject();
                                        break;
                                    case 1:
                                        //if(lstData.size() < 1)
                                        // ket qua cau lenh insert, delete, update

                                        kq = (Integer) oInput.readObject();
                                        if (kq == 0) {
                                            msg = (String) oInput.readObject();
                                        }
                                        break;
                                    case 3: 
                                        kqua =  (Vector<String>)oInput.readObject();
                                        lstThuocTinh = (Vector<Vector<String>>)oInput.readObject();
                                        //System.out.println(kqua.size());
                                        break;
                                        
                                        
                                }
                               // System.out.println(receivedMessage);
                            } catch (Exception e) {
                            }
                        }
                    }
                };
                t.start();
        } catch (Exception e) {
        }
    }
    
    public void sent(String massage){
        try {
            if (clientSocket != null) {
                  oOutput.writeObject(massage);
                  oOutput.flush();
            }
        } catch (Exception e) {
        }
    }


    public String getMessage(){
        return receivedMessage;
    }
    
}
